package org.bao.action;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.bao.bean.usermessageBean;
import org.bao.service.IRegisterService;
import org.bao.utils.MyUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * @ClassName: UserMessage
 * @Description: 注册用户信息,并修改用户信息
 * @author bql
 * @date 2019年4月30日
 *
 */
@Controller
public class UserRegister {

	@Resource
	private IRegisterService RegisterS;// 按名称匹配进行注入

	/**
	 * @Title: usermessage
	 * @Description: 接受用户信息，完成数据库登记
	 * @return void 返回类型
	 */
	@RequestMapping("/UserRegister")
	public void usermessage(usermessageBean userB, HttpServletRequest request, HttpServletResponse response)
			throws IOException {
		Integer count = 0;

		System.out.println("用户信息userB:" + userB.toString());
		System.out.println("用户信息request:" + request.getInputStream());
		
		MyUtils.uploadFilesWithRequest(request, userB, "userImgs", "userImg");

		// 当用户使用相同的手机号注册时会报错
		try {
			count = RegisterS.insertUserMessage(userB);
		} catch (Exception e) {
			System.out.println(e.getMessage());
		}
		// 判断是否成功
		response.setContentType("text/html;charset=utf-8");
		PrintWriter out = response.getWriter();
		// 根据service层操作结果进行跳转处理
		if (count > 0) {
			// 新增成功
			// 缓存登录账户的个人信息
			request.getSession().setAttribute("userTel", userB.getUsertel());
			request.getSession().setAttribute("userName", userB.getUsername());
			request.getSession().setAttribute("userPsw", userB.getUserpsw());
			
			//返回
			out.write("<script>window.location.href='jump?pg=userMessage/account';</script>");
		} else {
			System.out.println("!!2");
			out.write(
					"<script>var isBoolean=window.confirm('信息错误，请核对信息后重新输入!');if(isBoolean){window.location.href='jump?pg=userMessage/registe'};</script>");
		}
	}

	/**
	 * 根据电话号返回个人用户中心页面的数据
	 * 
	 * @param request
	 * @return
	 */
	@ResponseBody
	@RequestMapping("UserMessage")
	public List<usermessageBean> telAjaxQuery(HttpServletRequest request) {
		// 接受前台数据
		String tel = request.getParameter("usertel");
		System.out.println("个人用户中心页面接收到的电话:" + tel);
		// 创建一个实例，接受到的放电话
		usermessageBean userB = new usermessageBean(tel);
		List<usermessageBean> queryList = RegisterS.telAjaxQuery(userB);

		return queryList;
	}

	/**
	 * 修改用户信息
	 * 
	 * @param request
	 */
	@RequestMapping("ChangUsermessage")
	public String changpassword(usermessageBean userB, HttpServletRequest request) {
		// 获取前台数据
		String userpsw = request.getParameter("newuserpsw");

		// 获取用户电话
		String usertel = (String) (request.getSession().getAttribute("userTel"));
		// 放入对象中
		userB.setUsertel(usertel);
		userB.setUserpsw(userpsw);
		// 修改数据库信息
		int count = RegisterS.updataUserMessagePsw(userB);

		if (count == 1) {
			if (userB.getUserpsw() == null || userB.getUserpsw() == "") {
			}
			return "userMessage/perinfo";
		} else {
			return "userMessage/account";
		}

	}

}
